#include"queue.h"
Qnode* CreateNode(QeDataType x)
{
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void QueueInit(Qe* q)
{
	assert(q);
	Qnode* newnode = CreateNode(-1);
	q->head = newnode;
	q->back = newnode;
}
void QueueDestroy(Qe* q)
{
	assert(q);
	Qnode* cur = q->head->next;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
	/*free(q->back);*/
	q->back = NULL;
	free(q->head);
	q->head = NULL;
}
void Queuepush(Qe* q, QeDataType x)
{
	assert(q);
	Qnode* newnode = CreateNode(x);
	q->back->next = newnode;
	q->back = newnode;
}
void QueuePop(Qe* q)
{
	assert(q);
	assert(q->head->next);

	Qnode* next = q->head->next;
	q->head->next = next->next;
	free(next);
	next = NULL;
	if (q->head->next == NULL)
	{
		q->back = q->head;
		q->back->next = NULL;
	}
}
QeDataType QueueFront(Qe* q)
{
	assert(q);
	assert(q->head->next);
	return q->head->next->data;
}
QeDataType QueueBack(Qe* q)
{
	assert(q);
	assert(q->head->next);
	return q->back->data;
}
int QueueSize(Qe* q)
{
	Qnode* size = q->head->next;
	int num = 0;
	while (size)
	{
		size = size->next;
		num++;
	}
	return num;
}
bool QueueEmpty(Qe* q)
{
	return q->head->next == NULL;
}